package product;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.KeyStroke;

import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;

public class ProductMain3 extends JInternalFrame{
	String logincode = "";  //로그인한 사람의 사원번호


	String driver="oracle.jdbc.driver.OracleDriver";
	String url="jdbc:oracle:thin:@192.168.10.4:1521:xe";
	String db_user ="hr";
	String db_pwd="hr";
	Connection conn;	

	/*	HashMap<String, MemberData> mem = new HashMap<>();
	HashMap<String, TempData> chul = new HashMap<>();
	LinkedHashMap<String, ChulGData> chulG = new LinkedHashMap<>();
	HashMap<String, MemberData> holy = new HashMap<>();
	HashMap<String, ProductData> pd = new HashMap<String, ProductData>();
	 */
	ProductMain3 pm=this;
	//	CodeInput cInput = null;
	//	CodeUpdate cUpdate = null;
	ProductInput pInput = null;
	//	ProductUpdate pUpdate = null;

	MemberInput mInput = null;
	MemberUpdate mUpdate = null;
	MemberSelect Mselect = null;

	SemiChul semiC = null;
	ChulList cl = null;
	MemberMonth2 mm2 = null;

	MemberHoly mHoly = null;
	MemberThread mt=null;
	MemberMonth mMonth = null;

	MemberUpdateLogin mLogin =null;
	//MemberUpdateLogout mLogout =null;
	ObjectInputStream ois;
	MemberHolyselect mhSelect = null;
	HolyReason hr = null;
	HolyReason2 hr2 = null;
	MemberHolyEditor mhe = null;


	static String filename = "Untitled";
	boolean modifyFlag = false;

	JDesktopPane contentPane;
	public JMenuBar menuBar;

	int yn;
	public JMenu mnMember;
	public JMenuItem mntmMemberinput;
	public JMenuItem mntmMemberupdate;
	public JMenuItem mntmMemberinout;
	public JMenuItem mntmMemberholy;
	public JMenuItem mntmMembermonth;
	public JMenuItem mntmNewMenuItem;
	public JMenuItem mntmNewMenuItem_1;
	public JMenuItem mntmNewMenuItem_2;


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					ProductMain3 frame = new ProductMain3();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void ipchange(){
		//		데이터베이스 연결 테스트
		//		1)드라이버로딩
		try{
			Class.forName(driver).newInstance();				
			//2)Connection 객체 생성
			conn=DriverManager.getConnection(url, db_user,db_pwd);	
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void openmonth(){
		if(mMonth == null){
			mMonth = new MemberMonth(pm, conn);					
			mMonth.setBounds(0, 0, 1280, 400);						
		}
		contentPane.add(mMonth);
		mMonth.setVisible(true);
	}
	public void openinouthistory(){
		if(semiC == null){
			semiC = new SemiChul(pm, conn);						
			semiC.setBounds(900, 0, 270, 200);						
		}
		contentPane.add(semiC);
		semiC.setVisible(true);
	}
	public void openinput(){
		if(mInput == null){
			mInput = new MemberInput(conn,pm);						
			mInput.setBounds(500, 300, 400, 400);						
		}
		contentPane.add(mInput);
		mInput.setVisible(true);
	}
	public void openholy(){
		if(mHoly == null){
			mHoly = new MemberHoly(conn,pm);						
			mHoly.setBounds(930, 300, 400, 300);						
		}
		contentPane.add(mHoly);
		mHoly.setVisible(true);
	}
	public void openholyeditor(){
		if(mhe == null){
			mhe = new MemberHolyEditor(conn, pm);
			mhe.setBounds(100, 100, 450, 80);
			contentPane.add(mhe);
		}
		mhe.setVisible(true);
		mhe.toFront();
	}
	public void openLogin(){
		if(mLogin == null){
			mLogin = new MemberUpdateLogin(conn, pm);
			mLogin.setBounds(100, 100, 250, 150);
			contentPane.add(mLogin);
		}
		mLogin.setVisible(true);
		mLogin.toFront();
	}	
	//public void openLogout(){
	//	if(mLogout == null){
	//		mLogout = new MemberUpdateLogout(conn, this);
	//		mLogout.setBounds(100, 100, 250, 150);
	//		contentPane.add(mLogout);
	//	}
	//	mLogout.setVisible(true);
	//	mLogout.toFront();
	//}	


	public void openreason(){
		if(hr == null){
			hr = new HolyReason(conn, pm);
			mHoly.setR(hr);
			hr.setBounds(50, 50, 478, 367);
			contentPane.add(hr);
		}
		hr.setVisible(true);
		hr.toFront();
	}
	public void openreason2(){
		if(hr2 == null){
			hr2 = new HolyReason2(conn,pm);
			mHoly.setR2(hr2);
			hr2.setBounds(50, 50, 450, 300);
			contentPane.add(hr2);
		}
		hr2.setVisible(true);
		hr2.toFront();
	}
	public void openholyselect(){
		if(mhSelect == null){
			mhSelect = new MemberHolyselect(conn,pm,mHoly);
			mHoly.setH(mhSelect);
			mhSelect.setBounds(0, 0,  450, 300);
			contentPane.add(mhSelect);
			mhSelect.columnadd();
		}
		mhSelect.setVisible(true);
		mhSelect.toFront();
	}
	public void openupdate(){
		if(mUpdate==null){
			mUpdate = new MemberUpdate(conn,pm);
			Mselect.setD(mUpdate);
			mUpdate.setU(Mselect);
			mUpdate.setBounds(0, 300, 500, 300);
			contentPane.add(mUpdate);
		}
		mUpdate.setVisible(true);
		mUpdate.toFront();
	}
	public void openselect(){
		if(Mselect == null){
			Mselect= new MemberSelect(conn,pm);
			//Mselect.setD(mUpdate);
			//mUpdate.setU(Mselect);
			Mselect.setBounds(0, 0, 900, 300);	
			contentPane.add(Mselect);

			Mselect.columnadd();

		}
		Mselect.setVisible(true);
		Mselect.toFront();
	}
	public void closeholy(){
		if(mHoly != null){
			mHoly.setVisible(false);
		}
	}
	public void closeholyeditor(){
		if(mhe != null){
			mhe.setVisible(false);
		}
	}
	public void closeholyeditorselect(){
		if(mhSelect != null){
			mhSelect.setVisible(false);
		}
	}
	public void closereason(){
		if(hr != null){
			hr.setVisible(false);
		}
	}
	public void closereason2(){
		if(hr2 != null){
			hr2.setVisible(false);
		}
	}
	public ProductMain3() {
		super("3조-사원관리", true, true, true, false);
		//setTitle("Product Manage Program");
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		try {
			UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
			UIManager.put("InternalFrame.titleFont", new Font("Gulim", Font.PLAIN, 11));

		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		setBounds(100, 100, 1002, 693);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		ipchange();

		mt = new MemberThread(pm);
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnMember());
		}
		return menuBar;
	}
	private JMenu getMnMember() {
		if (mnMember == null) {
			mnMember = new JMenu("Member");
			mnMember.add(getMntmNewMenuItem_1());
			mnMember.add(getMntmNewMenuItem_2());
			mnMember.add(getMntmMemberinput());
			mnMember.add(getMntmMemberupdate());
			mnMember.add(getMntmMemberinout());
			mnMember.add(getMntmMemberholy());
			mnMember.add(getMntmMembermonth());
			mnMember.add(getMntmNewMenuItem());
		}
		return mnMember;
	}
	private JMenuItem getMntmMemberinput() {
		if (mntmMemberinput == null) {
			mntmMemberinput = new JMenuItem("사원 등록");
			mntmMemberinput.setVisible(false);
			mntmMemberinput.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK));
			mntmMemberinput.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if(mInput == null){
						mInput = new MemberInput(conn,pm);						
						mInput.setBounds(500, 0, 450, 300);						
					}
					contentPane.add(mInput);
					mInput.setVisible(true);
				}
			});
		}
		return mntmMemberinput;
	}
	private JMenuItem getMntmMemberupdate() {
		if (mntmMemberupdate == null) {
			mntmMemberupdate = new JMenuItem("사원 정보 수정");
			mntmMemberupdate.setVisible(false);
			mntmMemberupdate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));
			mntmMemberupdate.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					//if(mUpdate == null){
					//	mUpdate = new MemberUpdate(conn,pm);						
					//	mUpdate.setBounds(50, 300, 450, 320);						
					//}
					//contentPane.add(mUpdate);
					//mUpdate.setVisible(true);
					openselect();
				}
			});
		}
		return mntmMemberupdate;
	}
	private JMenuItem getMntmMemberinout() {
		if (mntmMemberinout == null) {
			mntmMemberinout = new JMenuItem("사원 출퇴근");
			mntmMemberinout.setVisible(false);
			mntmMemberinout.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
			mntmMemberinout.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {

					openinouthistory();
					semiC.loginname();
					semiC.chulbtn.setVisible(true);
					semiC.tbtn.setVisible(false);
				}
			});
		}
		return mntmMemberinout;
	}
	private JMenuItem getMntmMemberholy() {
		if (mntmMemberholy == null) {
			mntmMemberholy = new JMenuItem("사원 휴가");
			mntmMemberholy.setVisible(false);
			mntmMemberholy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
			mntmMemberholy.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {

					openholy();

				}
			});
		}
		return mntmMemberholy;
	}
	private JMenuItem getMntmMembermonth() {
		if (mntmMembermonth == null) {
			mntmMembermonth = new JMenuItem("출퇴근 월별 조회");
			mntmMembermonth.setVisible(false);
			mntmMembermonth.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.CTRL_MASK));
			mntmMembermonth.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {

					openmonth();

				}
			});
		}
		return mntmMembermonth;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("All");
			mntmNewMenuItem.setVisible(false);
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					openholy();
					openinput();
					openinouthistory();
					openselect();
					openupdate();
					openmonth();


				}
			});
			mntmNewMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
		}
		return mntmNewMenuItem;
	}

	public void openChulList(){
		if (cl == null) {
			cl = new ChulList(pm, semiC);
			cl.setBounds(273, 0, 450, 500);
			contentPane.add(cl);
		}
		cl.setVisible(true);
		cl.toFront();
		cl.dayhistory();
	}
	
	public void closeChulList(){
		if(cl != null){
			cl.setVisible(false);			
		}
	}
	
	public void openMemberMonth2(){
		if (mm2 == null) {
			mm2 = new MemberMonth2(semiC, pm);
			mm2.setBounds(723, 0, 314, 286);
			pm.contentPane.add(mm2);
		}
		mm2.setVisible(true);
		mm2.toFront();
		mm2.mm2chul();
	}
	
	public void closeMemberMonth2(){
		if(mm2 != null){
			mm2.setVisible(false);
			mm2.yearcombo.setSelectedIndex(0);
			mm2.monthcombo.setSelectedIndex(0);
			mm2.year = (String)mm2.yearcombo.getItemAt(0);
			mm2.month = (String)mm2.monthcombo.getItemAt(0);
			
		}
	}

	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("로그인");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openLogin();
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("로그아웃");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				//	btnLogout.setEnabled(true);
					int yn = JOptionPane.showConfirmDialog(null, "로그아웃 하시겠습니까?");
					if(yn == JOptionPane.NO_OPTION){
						JOptionPane.showMessageDialog(null, "로그아웃 취소 되었습니다.");
					}
					else if(yn == JOptionPane.YES_OPTION){
					JOptionPane.showMessageDialog(null, "로그아웃 되었습니다.");
					pm.mntmMemberupdate.setVisible(false);
					pm.mntmMemberupdate.setVisible(false);
					pm.mntmMemberholy.setVisible(false);
					pm.mntmMembermonth.setVisible(false);
					if(pm.mInput!=null){
						pm.mInput.setVisible(false);
					}
					if(pm.Mselect!=null){
						pm.Mselect.setVisible(false);
					}
					if(pm.mMonth!=null){
						pm.mMonth.setVisible(false);
					}
					if(pm.mHoly!=null){
						pm.mHoly.setVisible(false);
					}
					if(pm.semiC!=null){
						pm.semiC.setVisible(false);
					}
					//if(pm.mLogout!=null){
					//	pm.mLogout.setVisible(false);
					//}
					
					pm.mLogin.btnLogin.setEnabled(true);
					pm.mLogin.adPass.setEnabled(true);
					pm.mLogin.adId.setEnabled(true);
					pm.mLogin.adPass.setText("");	
					pm.mLogin.adId.setText("");
					pm.mLogin.tFlag=false;
					
				}
				}
			});
		}
		return mntmNewMenuItem_2;
	}
}
